// =============================================================
// CKeyValue
// ---------------------------------------
// Author : Woo-Hyun Kim
// Email : woorung@icu.ac.kr or woorung@nhncorp.com
// Date : Sep. 4, 2007
// =============================================================
#include "CKeyValue.h"

CKeyValue::CKeyValue() 
{ 
	key = NULL; 
	value = NULL;
}

CKeyValue::CKeyValue(const CKeyValue &obj) 
{
	key = new char[strlen((char *)obj.key)+1];
	strcpy((char *)key, (char *)obj.key);

	value = new char[strlen((char *)obj.value)+1];
	strcpy((char *)value, (char *)obj.value);
}

CKeyValue::~CKeyValue() 
{
	if((char *)key!=NULL) delete []key;
	if((char *)value!=NULL) delete []value;
}

bool CKeyValue::blindlyEquals(const CObject *rhs) const 
{
#ifdef DEBUG
cout << "[CKeyValue] blindlyEquals" << endl;
#endif
	const CKeyValue *casted_rhs = dynamic_cast<const CKeyValue *>(rhs);
	// perform comparisons on private message
	bool isEqual = (strcmp(this->key, casted_rhs->key)==0);
	return (isEqual&&CObject::blindlyEquals(rhs));
}

const type_info *CKeyValue::getTypeEquiv() const 
{
#ifdef DEBUG
cout << "[CKeyValue] typeid name = " << typeid(this).name() << endl;
#endif

	return &typeid(this);
}


CKeyValue *CKeyValue::clone() const 
{
	return new CKeyValue(*this);
}

CKeyValue &CKeyValue::operator=(CKeyValue &obj) 
{
	// identity test
	if(this==&obj) return *this;

	key = new char[strlen((char *)obj.key)+1];
	strcpy((char *)key, (char *)obj.key);

	value = new char[strlen((char *)obj.value)+1];
	strcpy((char *)value, (char *)obj.value);

	return *this;
}

int CKeyValue::set(char *k, char *v) 
{
	if((char *)key!=NULL) {
		delete []key;
		key = NULL;
	}
	if((char *)value!=NULL) {
		delete []value;
		value = NULL;
	}

	key = new char[strlen(k)+1];
	strcpy((char *)key , (char *)k);

	value = new char[strlen(v)+1];
	strcpy((char *)value , (char *)v);

	return strlen((char *)key);
}

char *CKeyValue::getKey() 
{ 
	return (char *) key; 
}

char *CKeyValue::getValue() 
{ 
	return (char *) value; 
}

void CKeyValue::setKey(char *k)
{
	if((char *)key!=NULL) {
		delete []key;
		key = NULL;
	}

	key = new char[strlen(k)+1];
	strcpy((char *)key , (char *)k);
}

void CKeyValue::setValue(char *v)
{
	if((char *)value!=NULL) {
		delete []value;
		value = NULL;
	}

	value = new char[strlen(v)+1];
	strcpy((char *)value , (char *)v);
}

void CKeyValue::print() 
{ 
	cout << key << "=" << value << endl; 
}
